package com.yxq.mdt.app.base;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;

import com.gyf.immersionbar.ImmersionBar;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.AutoDisposeConverter;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.ibase.IBaseContract;
import com.yxq.mdt.app.base.ibase.IBaseFragment;
import com.yxq.mdt.app.constant.Constant;
import com.yxq.mdt.app.dialog.CostumerDialog;
import com.yxq.mdt.app.glide.GlideHelper;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * ================================================
 *
 * @function BaseFragment
 * Created by yd on 2019/02/15.
 * ================================================
 */
public abstract class BaseFragment<P extends IBaseContract.IBasePresenter> extends Fragment implements IBaseFragment {
    protected final String TAG = this.getClass().getSimpleName();
    protected Context mContext;
    private int statusBarColor = R.color.white;
    @Nullable
    protected P mPresenter;//如果当前页面逻辑简单, Presenter 可以为 null
    protected Unbinder unbinder;
    public ImmersionBar mImmersionBar;
    View view;
    private CostumerDialog mLoadingDialog;
    private boolean isImmersionBarEnabled = true;
    protected  int page=1;
    /**
     * 绑定布局文件
     *
     * @return 布局文件ID
     */
    protected abstract int attachLayoutId();

    /**
     * 设置 mPresenter
     */
    protected abstract P getPresenter();

    /**
     * 是否在Fragment使用沉浸式
     *
     * @return the boolean
     */
    public Boolean isImmersionBarEnabled() {
        return isImmersionBarEnabled;
    }

    /**
     * 设置状态栏颜色 默认值colorAccent
     *
     * @param statusBarColor
     */
    public void setStatusBarColor(int statusBarColor) {
        this.statusBarColor = statusBarColor;
    }

    /**
     * 初始化沉浸式
     */
    public void initImmersionBar() {
        mImmersionBar.reset()
                .fitsSystemWindows(true)// 使用该属性,必须指定状态栏颜色
                .statusBarColor(statusBarColor)
                .autoDarkModeEnable(true)
                .init();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = getPresenter();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mImmersionBar = ImmersionBar.with(this);
        if (isImmersionBarEnabled()) {
            initImmersionBar();
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && mImmersionBar != null) {
            initImmersionBar();
        }
    }

    /**
     * 绑定生命周期
     */
    public <X> AutoDisposeConverter<X> bindAutoDispose() {
        return AutoDispose.autoDisposable(AndroidLifecycleScopeProvider
                .from(this, Lifecycle.Event.ON_DESTROY));
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(mContext).inflate(attachLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(view);
        initData();
    }

    /**
     * Toast显示在正中间
     *
     * @param msg
     */
    protected void showCenterToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            this.mPresenter = null;
        }
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mContext = null;
    }

    /**
     * 显示通用的广告对话框
     */
    public void showCommonAdDialog(String imgUrl) {

        CostumerDialog adDialog = new CostumerDialog.Builder(mContext, R.style.custom_dialog_no_match)
                .setContentView(R.layout.dialog_common_ad_layout)
                .setCancelable(false)
                .create();

        AppCompatImageView adImage = adDialog.findViewById(R.id.iv_ad_image);
        GlideHelper.load(mContext, imgUrl, adImage);

        AppCompatImageView closeImage = adDialog.findViewById(R.id.iv_close);
        closeImage.setOnClickListener(new CustomClickListener() {
            @Override
            public void onSingleClick() {
                adDialog.dismiss();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mLoadingDialog != null) {
            if (mLoadingDialog.isShowing()) {
                mLoadingDialog.dismiss();
            }
            mLoadingDialog = null;
        }
    }

    protected void showLoadingDialog() {
        if (mLoadingDialog == null)
            mLoadingDialog = new CostumerDialog.Builder(mContext, R.style.custom_dialog_no_match)
                    .setContentView(R.layout.dialog_common_loading_layout)
                    .setCancelable(false)
                    .create();
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    protected void hideLoadingDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    public void setImmersionBarEnabled(boolean immersionBarEnabled) {
        isImmersionBarEnabled = immersionBarEnabled;
    }

    public String getParentId() {
        return !TextUtils.isEmpty(Constant.getLoginUser().getBinding()) ? Constant.getLoginUser().getBinding() : "0";
    }
}
